/**
 * Created by hy1 on 2017/12/24.
 */
/**
 * Created by hy1 on 2017/12/24.
 */
var baseUrl="";
var baseUrlHL='';
var bigkey='';
if(!sessionStorage.getItem("PROD")){
    sessionStorage.setItem("PROD",1);
}
var Btc=0,Eth= 0,Ltc= 0,Xrp=0;
//5454
var  type=sessionStorage.getItem("PROD");
if(!type){
    type=1;
}
var kstatus=sessionStorage.getItem("stockid");
if(!kstatus){
	kstatus=2;
}
//var bigkey="BSbtcusd";
var myChart = echarts.init(document.getElementById('Echarts1'));
var symbol=sessionStorage.getItem("tokenSymbol");
symbol?symbol:"btcusdt";
//标题
$().ready(function(){
	var title=symbol.split("usdt")[0].toUpperCase();
	$(".Container_Box .text-center").html(title+"/USDT")
})
function canvasKimg  (url) {
    var _this=this;
    //分钟柱子
    clearInterval(this.OneTime);
    //先执行一次 请求，
    $.ajax({
    	url:"http://api.huobipro.com/market/history/kline?period=1min&size=100&symbol="+symbol,
//    	url:"/app/public/klin",
//    	data:{url:Url},
        dataType: "json",//返回格式为json
        async: true,//请求是否异步，默认为异步，这也是ajax重要特性
        type: "get",   //请求方式
        success: function (r) {
        	//return true;
        	var btcNewPrice=r.data[0].open;
             //请求成功时处理
            var time = [];
            var timeEnd = [];     //结束时间
            var timeStart = [];   //开始时间
            var b = [];
            var da = new Array();
            console.log("length",r.data.length);
            //timeStart = (r.datas[r.datas.length-1].creattime);
            //timeEnd = (r.datas[0].creattime);
            $(".color-six").html("成交量："+r.data[0].vol);
            //for (var i =0; i < r.datas.length; i++) {
            for (var i =0; i <r.data.length; i++) {
//                var time = r.data[i].time;  
//                var str1 = time.replace('-', '/');
                var str2 = 2012+i;
                var da = new Array();
                var NewPrice = [];
                da.push(str2);
                da.push(parseFloat(r.data[i].open))
                da.push(parseFloat(r.data[i].close))
                da.push(parseFloat(r.data[i].low))
                da.push(parseFloat(r.data[i].high))
                b.push(da);
            }
            var data0 = splitData(b.reverse());
            //切割数组，把数组中的日期和数据分离，返回数组中的日期和数据
            function splitData(rawData) {
                var categoryData = [];
                var values = [];
                for (var i = 0; i < rawData.length; i++) {
                    //splice 返回每组数组中被删除的第一项，即返回数组中被删除的日期
                    //categoryData  日期  把返回的日期放到categoryData[]数组中
                    categoryData.push(rawData[i].splice(0, 1)[0]);
                    //alert(categoryData);
                    //数据数组，即数组中除日期外的数据
                    values.push(rawData[i])
                }
                return {
                    categoryData: categoryData, //数组中的日期 x轴对应的日期
                    values: values              //数组中的数据 y轴对应的数据
                };
            }
            var closes = [];
            for (var i = 0; i < b.length; i++) {
                closes.push(b[i][1]);
            }
            //计算MA平均线，N日移动平均线=N日收盘价之和/N  dayCount要计算的天数(5,10,20,30)
            function calculateMA(dayCount) {
                var result = [];
                for (var i = 0, len = data0.values.length; i < len; i++) {
                    if (i < dayCount) {
                        result.push('-');
                        //alert(result);
                        continue;   //结束单次循环，即不输出本次结果
                    }
                    var sum = 0;
                    for (var j = 0; j < dayCount; j++) {
                        //收盘价总和
                        sum += data0.values[i - j][1];
                    }
                    result.push(sum / dayCount);
                }
                return result;
            }
            //},1000);
            //线条
            /**
             * 计算boll指标,ma的周期为20日
             * @method BOLL
             * @param {Array} ticks
             * 一维数组类型，每个元素为当前Tick的收盘价格
             * @return {Object} 返回一个包含upper mid lower属性的对象,每个属性对应的类型为{Array[Number]}
             */
            var bollc = boss(closes);
            var low =(bollc.lower[bollc.lower.length-1]).toFixed(2);
            var mid =(bollc.mid[bollc.mid.length-1]).toFixed(2);
            var upper =(bollc.upper[bollc.upper.length-1]).toFixed(2);

            function boss(ticks) {
                //移动平均线周期为20
                var maDays = 20, tickBegin = maDays - 1, maSum = 0, p = 0;
                var ups = [], mas = [], lows = [];
                for (var i = 0; i < ticks.length; i++) {
                    var c = ticks[i], ma, md, bstart, mdSum;
                    maSum += c;
                    if (i >= tickBegin) {
                        maSum = maSum - p;
                        ma = maSum / maDays;
                        mas.push(ma);
                        bstart = i - tickBegin;
                        p = ticks[bstart];
                        mdSum = ticks.slice(bstart, bstart + maDays).reduce(function (a, b) {
                            return a + Math.pow(b - ma, 2);
                        }, 0);
                        md = Math.sqrt(mdSum / maDays);
                        ups.push(ma + 2 * md);
                        lows.push(ma - 2 * md);
                    } else {
                        //ugly constant, just keep the same type for client     //-1
                        ups.push(low);
                        mas.push(mid);
                        lows.push(upper);
                    }
                }
                return {"upper": ups, "mid": mas, "lower": lows};
            };
            var hhh =boss(closes);
            var x = calculateMA(5), y = calculateMA(10), z = calculateMA(20), w = calculateMA(30), v = calculateMA(50);
            var bollupper = bollc.upper;
            var bollmid = bollc.mid;
            var bolllower = bollc.lower;

            for (var t = [], a = [], n = [], e = [], s = [], qq = [], r = [], upper = [], mid = [], lower = [], i = 30; i <b.length; i++) {
                t.push(i),
                    a.push(b[i]);
                n.push(x[i]);
                e.push(z[i]);
                s.push(y[i]);
                qq.push(w[i]);
                r.push(v[i]);
                upper.push(bollupper[i]);
                mid.push(bollmid[i]);
                lower.push(bolllower[i]);
            }
            var o = [];
            var o = [{
                data: n,
                color: "#FFFFFF"
            },
                {
                    data: e,
                    color: "#F4FD6E"
                },
                {
                    data: s,
                    color: "#D643D9"
                },
                {
                    data: qq,
                    color: "#0781F3"
                },
                {
                    data: r,
                    color: "green"
                },
                {
                    data: upper,
                    color: "#F5FB00"
                },
                {
                    data: mid,
                    color: "#81CB89"
                },
                {
                    data: lower,
                    color: "#FFAA00"
                }
            ];
            var showline = parseInt(window.showEcharts % 2);

            // 数据意义：开盘(open)，收盘(close)，最低(lowest)，最高(highest)
            if (showline == 1) {
                //3条线
                o = [{data: upper, color: "#F4FD6E"},
                    {data: mid, color: "#fff"},
                    {data: lower, color: "#D643D9"},
                    {data: r, color: "transparent"}];
                //  MA5
                $(".color-first").html("BOLL(20.2)").css("color", "#5D6D6F")
                $(".color-two").html("BOLL:" + (hhh.mid[hhh.mid.length-1]).toFixed(2)).css("color", "#fff");
                $(".color-three").html("UPPER:" + (hhh.upper[hhh.upper.length-1]).toFixed(2)).css("color", "#FFFF00");
                $(".color-four").html("LOWER:" + (hhh.lower[hhh.lower.length-1]).toFixed(2)).css("color", "#CB3CA5");
                $(".color-five").css("display","none");

            } else {
                //4条线
                o = [{data: n, color: "#FFFFFF"},
                    {data: e, color: "#D643D9"},
                    {data: s, color: "#F1F119"},
                    {data: qq, color: "#0CF601"}
                    //{data: r, color: "#7A7B7D"}
                ]
                $(".color-first").html("MA5:" + (x[x.length - 1]).toFixed(2)).css("color", "#fff");
                $(".color-two").html("10:" + (y[y.length - 1]).toFixed(2)).css("color", "#FFFF00");
                $(".color-three").html("20:" + (z[z.length - 1]).toFixed(2)).css("color", "#CB3CA5");
                $(".color-four").html("30:" + (w[w.length - 1]).toFixed(2)).css("color", "#0CF601");
                $(".color-five").html("50:" + (v[v.length - 1]).toFixed(2)).css("color", "#7A7B7D");
                $(".color-five").css("display", "block");
               
            }
            //time
            //$(".time_start").html((timeStart.slice(0,16)));
            //$(".time_end").html((timeEnd.slice(0,16)));
            initCharts(t, a, o,btcNewPrice);//new write
            $('.coin_price').html(btcNewPrice);                        //最新价格
           /*console.log("new price",r.data)
            var Btc=r.data[1].close;
	          if(Btc===0){
	              Btc=r.data[0].close;
	          }else{
	              if(r.data[0].close>Btc){
	                  $(".coin_price").css("color","#EA392F");//red
	              }else if(r.data[0].close<Btc){
	                  $(".coin_price").css("color","#12D31F"); //green
	              }
	              //Btc=r.data[0].close;
	          }*/
            //最新价格 实时波动想 数据
            var NewPrice=[];
            var   sid=sessionStorage.getItem("stockid");
//            $.ajax({
//                url:"http://api.huobipro.com/market/history/kline?period=1day&size=100&symbol="+symbol,
//                dataType: "json",       //
//                async: true,           //请求是否异步，
//                type: "GET",            //请求方式
//                success:function(result){
//                	console.log("new price",result)
//                    var data =result.datas;
//                    NewPrice.push(data[0].close);
//                    var price=data[0].close;
//                    var sid=sessionStorage.getItem("stockid");
//                   /* if(sid!=11&&sid!=14){
//                   	 price=returnFloat(price);
//                   }*/
//                    $('.coin_price').html(price);                        //最新价格
//                    if(Btc===0){
//                        Btc=data[0].close;
//                    }else{
//                        if(data[0].close>Btc){
//                            $(".coin_price").css("color","#EA392F");//red
//                        }else if(data[0].close<Btc){
//                            $(".coin_price").css("color","#12D31F"); //green
//                        }
//                        Btc=data[0].close;
//                    }
//                    initCharts(t, a, o,NewPrice);
//                },
//            });
            
           
          
        }
    });
    this.OneTime = setInterval(function(){
    	var hight="http://api.huobipro.com/market/detail/merged?symbol="+symbol;
    	$.get(hight,function(r){
    		var data=JSON.parse(r);
    		data=data.tick;
    		var high=data.high.toFixed(2),low=data.low.toFixed(2),vol=data.vol.toFixed(2);
    		$(".shares-info .high").text(high),$(".shares-info .low").text(low),$(".shares-info .volume").text(vol)
    	})
    	
        $.ajax({
            url: "http://api.huobipro.com/market/history/kline?period=1min&size=100&symbol="+symbol,
//            data:{url:Url},
            dataType: "json",//返回格式为json
            async: true,//请求是否异步，默认为异步，这也是ajax重要特性
            type: "GET",   //请求方式
            success: function (r) {
            	//console.log("time inter ",r)
                //请求成功时处理
                var time = [];
                var timeEnd = [];     //结束时间
                var timeStart = [];   //开始时间
                var b = [];
                var da = new Array();
                $(".color-six").html("成交量："+r.data[0].vol);
                for (var i = 0; i < r.data.length; i++) {
                    /*var time = r.data[i].time;
                    timeStart = (r.data[i].creattime);
                    timeEnd = (r.data[0].creattime);*/
                    //var str1 = time.replace('-', '/');
                    var str2 = 2013+i;
                    var da = new Array();
                    var NewPrice = [];
                    da.push(str2);
                    da.push(parseFloat(r.data[i].open))
                    da.push(parseFloat(r.data[i].close))
                    da.push(parseFloat(r.data[i].low))
                    da.push(parseFloat(r.data[i].high))
                    b.push(da);
                }
                var data0 = splitData(b.reverse());

                //切割数组，把数组中的日期和数据分离，返回数组中的日期和数据
                function splitData(rawData) {
                    var categoryData = [];
                    var values = [];
                    for (var i = 0; i < rawData.length; i++) {
                        //splice 返回每组数组中被删除的第一项，即返回数组中被删除的日期
                        //alert(rawData[i].splice(0, 1)[0]);
                        //categoryData  日期  把返回的日期放到categoryData[]数组中
                        categoryData.push(rawData[i].splice(0, 1)[0]);
                        //alert(categoryData);
                        //数据数组，即数组中除日期外的数据
                        // alert(rawData[i]);
                        values.push(rawData[i])
                    }

                    return {
                        categoryData: categoryData, //数组中的日期 x轴对应的日期
                        values: values              //数组中的数据 y轴对应的数据
                    };
                }
                var closes = [];
                for (var i = 0; i < b.length; i++) {
                    closes.push(b[i][1]);
                }
                //计算MA平均线，N日移动平均线=N日收盘价之和/N  dayCount要计算的天数(5,10,20,30)

                function calculateMA(dayCount) {
                    var result = [];
                    for (var i = 0, len = data0.values.length; i < len; i++) {
                        if (i < dayCount) {
                            result.push('-');
                            //alert(result);
                            continue;   //结束单次循环，即不输出本次结果
                        }

                        var sum = 0;
                        for (var j = 0; j < dayCount; j++) {
                            //收盘价总和
                            sum += data0.values[i - j][1];
                        }

                        result.push(sum / dayCount);
                        // alert(result);
                    }
                    return result;
                }
                //},1000);
                //线条
                /**
                 * 计算boll指标,ma的周期为20日
                 * @method BOLL
                 * @param {Array} ticks
                 * 一维数组类型，每个元素为当前Tick的收盘价格
                 * @return {Object} 返回一个包含upper mid lower属性的对象,每个属性对应的类型为{Array[Number]}
                 */
                var bollc = boss(closes);
                var low =(bollc.lower[bollc.lower.length-1]).toFixed(2);
                var mid =(bollc.mid[bollc.mid.length-1]).toFixed(2);
                var upper =(bollc.upper[bollc.upper.length-1]).toFixed(2);
                function boss(ticks) {
                    //移动平均线周期为20
                    var maDays = 20, tickBegin = maDays - 1, maSum = 0, p = 0;
                    var ups = [], mas = [], lows = [];
                    for (var i = 0; i < ticks.length; i++) {
                        var c = ticks[i], ma, md, bstart, mdSum;
                        maSum += c;
                        if (i >= tickBegin) {
                            maSum = maSum - p;
                            ma = maSum / maDays;
                            mas.push(ma);
                            bstart = i - tickBegin;
                            p = ticks[bstart];
                            mdSum = ticks.slice(bstart, bstart + maDays).reduce(function (a, b) {
                                return a + Math.pow(b - ma, 2);
                            }, 0);
                            md = Math.sqrt(mdSum / maDays);
                            ups.push(ma + 2 * md);
                            lows.push(ma - 2 * md);
                        } else {
                            //ugly constant, just keep the same type for client     //-1
                            ups.push(low);
                            mas.push(mid);
                            lows.push(upper);
                        }
                    }
                    return {"upper": ups, "mid": mas, "lower": lows};
                };
                var hhh =boss(closes);
                var x = calculateMA(5), y = calculateMA(10), z = calculateMA(20), w = calculateMA(30), v = calculateMA(50);
                var bollupper = bollc.upper;
                var bollmid = bollc.mid;
                var bolllower = bollc.lower;

                for (var t = [], a = [], n = [], e = [], s = [], qq = [], r = [], upper = [], mid = [], lower = [], i = 30; i <b.length; i++) {
                    t.push(i),
                        a.push(b[i]);
                    n.push(x[i]);
                    e.push(z[i]);
                    s.push(y[i]);
                    qq.push(w[i]);
                    r.push(v[i]);
                    upper.push(bollupper[i]);
                    mid.push(bollmid[i]);
                    lower.push(bolllower[i]);
                }
                var o = [];
                var o = [{
                    data: n,
                    color: "#FFFFFF"
                },
                    {
                        data: e,
                        color: "#F4FD6E"
                    },
                    {
                        data: s,
                        color: "#D643D9"
                    },
                    {
                        data: qq,
                        color: "#0781F3"
                    },
                    {
                        data: r,
                        color: "green"
                    },
                    {
                        data: upper,
                        color: "#F5FB00"
                    },
                    {
                        data: mid,
                        color: "#81CB89"
                    },
                    {
                        data: lower,
                        color: "#FFAA00"
                    }
                ];
                var showline = parseInt(window.showEcharts % 2);
                // 数据意义：开盘(open)，收盘(close)，最低(lowest)，最高(highest)
                if (showline == 1) {
                    //3条线
                    o = [{data: upper, color: "#F4FD6E"},
                        {data: mid, color: "#fff"},
                        {data: lower, color: "#D643D9"},
                        {data: r, color: "transparent"}];
                    //  MA5
                    $(".color-first").html("BOLL(20.2)").css("color", "#5D6D6F")
                    $(".color-two").html("BOLL:" + (hhh.mid[hhh.mid.length-1]).toFixed(2)).css("color", "#fff");
                    $(".color-three").html("UPPER:" + (hhh.upper[hhh.upper.length-1]).toFixed(2)).css("color", "#FFFF00");
                    $(".color-four").html("LOWER:" + (hhh.lower[hhh.lower.length-1]).toFixed(2)).css("color", "#CB3CA5");
                    $(".color-five").css("display","none");

                } else {
                    //4条线
                    o = [{data: n, color: "#FFFFFF"},
                        {data: e, color: "#D643D9"},
                        {data: s, color: "#F1F119"},
                        {data: qq, color: "#0CF601"}
                        //{data: r, color: "#7A7B7D"}
                    ]
                    //第一个KLINE 数据
                    $(".color-first").html("MA5:" + (x[x.length - 1]).toFixed(2)).css("color", "#fff");
                    $(".color-two").html("10:" + (y[y.length - 1]).toFixed(2)).css("color", "#FFFF00");
                    $(".color-three").html("20:" + (z[z.length - 1]).toFixed(2)).css("color", "#CB3CA5");
                    $(".color-four").html("30:" + (w[w.length - 1]).toFixed(2)).css("color", "#0CF601");
                    $(".color-five").html("50:" + (v[v.length - 1]).toFixed(2)).css("color", "#7A7B7D");
                    $(".color-five").css("display", "block");
                    //time
                    $(".time_start").html((timeStart.slice(0,16)));
                    $(".time_end").html((timeEnd.slice(0,16)));
                    //console.log("Time====="+timeEnd.slice(0,16));
                }
                //time
                //最新价格 实时波动想 数据
                var NewPrice=[];
                var   sid=sessionStorage.getItem("stockid");
                $.ajax({
                    url:"http://api.huobipro.com/market/history/kline?period=1min&size=100&symbol="+symbol,
//                    url:"/app/public/klin",
//                    data:{url:Url},
                	dataType: "json",       //
                    async: true,           //请求是否异步，
                    type: "GET",            //请求方式
                    success:function(result){
                        var data =result.data;
                      //  console.log("HUOBI",data)
                        NewPrice.push(data[0].close);
                        var price=data[0].close;
                        /*var sid=sessionStorage.getItem("stockid");
                        if(sid!=11&&sid!=14){
                       	 price=returnFloat(price);
                       }*/
                        $('.coin_price').html(price);                        //最新价格
                        if(Btc===0){
                            Btc=data[0].close;
                        }else{
                            if(data[0].close>Btc){
                                $(".coin_price").css("color","#EA392F");//red
                            }else if(data[0].close<Btc){
                                $(".coin_price").css("color","#12D31F"); //green
                            }
                            Btc=data[0].close;
                        }
                        //绘制K线
                        initCharts(t,a,o,NewPrice);
                    },
                  error:function(r){}
                });

                //initCharts(t,a,o,NewPrice);
            }
        });
    },4000)
}

var initCharts=function (t, a, n, e) {
   
    for (var o = [], r = 0, c = n.length; r < c; r++) {
        var l = {
            type: "line",
            data: n[r].data,
            //zlevel: 0,
            smooth: !0,
            symbol: "none",
            //数据曲线
            itemStyle: {
                normal: {
                    lineStyle: {
                        width: 1,
                        color: n[r].color
                    },
                },
            },
        };
        0 == r && e && (
            l.markLine = {
                symbol: "none",
                data: [{
                    yAxis: e,
                }],
                //X直线
                lineStyle: {
                    normal: {
                        left:30,
                        color: "#fff",
                        type: "none",

                    },
                },
            })
        o.push(l)
    }
    //k线图设置
    o.push({
        type: "candlestick",
        data: a,
        barWidth: "70%",
        //zlevel: 1,
        markPoint: {
            symbol: "pin",
            symbolSize:'none',
            data: [{
                type: "max",
                name: "最大值",
                color: "#fff",
                position: "top",
                //zlevel: 99999,
                //symbol:'none',
                valueDim: 'highest'
            },
                {
                    type: "min",
                    name: "最小值",
                    position: "bottom",
                    valueDim: 'lowest'
                }
            ],
        },
        itemStyle: {
            //柱子
            type: 'candlestick',
            name: 'dka',
            data:t,
            normal: {
                color: "#1F2227",
                color0: "#0CF601",
                borderColor: "#DC2625",
                borderColor0: "#0CF601",
            },
        }
    }),
        //背景颜色
    myChart.setOption({
            animation: !1,
            backgroundColor: "#181B2A",
            grid: {
                show: !0,
                top: 0,
                left: -40,
                right: "10%",
                bottom: 3,
                height:'auto',
            },
            //最大.小值
            textStyle:{
                fontSize:9,
                color: "#fff",
            },
            xAxis: {
                scale: !0,
                data: t,
                progressive:60,
                boundaryGap: !0,
                axisTick: {show: false},
                splitLine: {
                    lineStyle: {
                        color: "#fff",
                    },
                    show: !1,
                },
                
            },
            yAxis: {
                scale: !0,
                position: "right",
                axisLabel: {
                    color: "#333",  //刻度线标签颜色
                    margin:1,
                    inside:false,
                    fontSize:9,
                    align:'left',
                    verticalAlign:'top',
                },
                //y轴 刻度尺显示
                axisTick: {
                    show: false,
                },
                splitLine: {
                    show: !0,
                    lineStyle: {
                        color: "#5D5D5D",
                        type: "solid",
                        width: 0.5,
                    }
                },
            },
            axisTick: {show: false},
            axisLine: {show: true},
            dataZoom: [{
                type: "inside",
                throttle:100 ,
            }],
            series:o,
        });
    myChart.group = "group1";
    echarts.connect("group1");
}
var echart2=echarts.init(document.getElementById("Echarts2"));
var initCharts2=function (t, a) {
    if(!echart2){
        echart2= echarts.init(document.getElementById("Echarts2"));
    }
    var bb=[];
    $.ajax({
        url:"http://api.huobipro.com/market/history/kline?period=1day&size=100&symbol="+symbol,
    	/*url:"/app/public/klin",
    	data:{url:Url},*/
        dataType: "json",   //返回格式为json
        async: false,//请求是否异步，默认为异步，这也是ajax重要特性
        type: "GET",   //请求方式
        success: function (r) {
            //请求成功时处理
            var len=r.data.length-30;
            for (var i = 0; i <len; i++) {
                /* bb[i]={};
                 bb[i].open = r.datas[i].open;
                 bb[i].close = r.datas[i].close;*/
                var data=r.data[i];
                var cc= data.open>data.close?1:-1;
                bb.push(cc);
            }
            bb.reverse();
        }
    });
    this.echart2.setOption({
        animation: !1,
        //边框边线
        grid: {
            show: !0,
            top: 0,
            left: -40,
            right: "10%",
            bottom:0
        },
        xAxis: [{
            type: "category",
            data: t,
            axisLine: {
                show: true,
                lineStyle: {color: "transparent"}
            },
            //inverse:{default:false},
        }],
        yAxis: {
            scale: !0,
            position: "right",
            axisLabel: {
                color: "#333",  //刻度线标签颜色
                margin:1,
                inside:false,
                fontSize:10,
                align:'left',
                verticalAlign:'top',
            },
            splitLine: {
                show: !1,
                lineStyle: {
                    //数据线条 虚线
                    //color: "#000",
                    color: "transparent",
                    type: "solid"
                }
            },
            //刻度尺 Y echarts 1
            axisTick: {
                show: false,
            },
            //right Y line
            axisLine: {show: true,}
        },

        dataZoom: [{
            type: "inside",
            //start:40,
            //end:100,
        }],
        series: [{
            type: "bar",
            barWidth: "70%",
            //barMaxWidth: "1rem",
            data: a,
            itemStyle: {
                normal: {
                    color: function (t) {
                        //var result= (parseFloat(bb[t.dataIndex].open)>parseFloat(bb[t.dataIndex].close))
                        return (bb[t.dataIndex]>0? "#0CF601" : "#E4393C");  //绿   红
                    },
                    borderColor: function (t) {
                        return (bb[t.dataIndex]>0 ? "transparent":"#DC2625");  //  背景 红
                    },
                    borderWidth: 1,
                    opacity: 1
                }
            }
        }]
    });
    echart2.group = "group1";
    echarts.connect("group1");
}
var echart3=echarts.init(document.getElementById("Echarts3"));
var initCharts3=function (t, a, n) {
    for (var e = [{
        type: "bar",
        barWidth: "40%",
        barMaxWidth: 1,
        //zlevel: 1,
        data: a,
        itemStyle: {
            normal: {
                color: function (t) {
                    return t.data < 0 ? "#0CF601" : "#DC2625";
                },
                //color:"red",
                //borderColor: function (t) {
                //    return t.data < 0 ? "#0CF601" : "#DC2625";
                //},
                borderWidth: 5
            }
        }
    }], i = 0, o = n.length; i < o; i++) e.push({
        type: "line",
        //zlevel: 999,
        data: n[i].data,
        smooth: !0,
        symbol: "none",
        lineStyle: {
            normal: {
                width: 1,
                color: n[i].color
            }
        }
    });
    echart3.setOption({
        animation: !1,
        backgroundColor:"#181B2A",
        grid: {
            show: !0,
            top: 0,
            left: -1,
            right: "10%",
            bottom: -1
        },
        xAxis: [{
            type: "category",
            axisTick: {
                alignWithLabel: !0
            },
            data: t
        }],
        yAxis: {
            scale: !0,
            position: "right",
            axisLabel: {
                color: "#333",  //刻度线标签颜色
                margin:1,
                inside:false,
                fontSize:9,
                align:'left',
                verticalAlign:'top',
            },
            splitLine: {
                show: !1,
                lineStyle: {
                    //数据线条 虚线
                    color: "#5D5D5D",
                    //color: "transparent",
                    type: "solid"
                }
            },
            //刻度尺 Y echarts 1
            axisTick: {
                show: false,
            },
            //right Y line
            axisLine: {show: true,}
        },
        dataZoom: [{
            type: "inside",
            //start:40,
            //end:100,
        }],
        series: e
    })
    echart3.group = "group1";
    echarts.connect("group1");
}

var canvasImg3=function (url) {
    var _this=this;
    clearInterval(this.ThreeTime);
    $.ajax({
        dataType: "json",
        url:url,
        /*url:"/app/public/klin",
    	data:{url:Url},*/
        async: true,//请求是否异步，默认为异步，
        type: "GET",   //请求方式
        success: function (r) {
            //return;
            //请求成功时处理
            var b = [];
            var da = new Array();
            for (var i = 0; i < r.data.length-30; i++) {
            	 /*var time = r.datas[i].creattime;
                var str1 = time.replace('_', '/');
                var str2 = str1.replace('_', '/');*/
            	var str2=2018+i;
                var da = new Array();
                da.push(str2);
                da.push(parseFloat(r.data[i].open))
                da.push(parseFloat(r.data[i].close))
                da.push(parseFloat(r.data[i].low))
                da.push(parseFloat(r.data[i].high))
                b.push(da);
            }
            var data0 = splitData(b.reverse());
            //切割数组，把数组中的日期和数据分离，返回数组中的日期和数据
            function splitData(rawData) {
                var categoryData = [];
                var values = [];
                for (var i = 30; i < rawData.length; i++) {
                    //splice 返回每组数组中被删除的第一项，即返回数组中被删除的日期
                    //alert(rawData[i].splice(0, 1)[0]);
                    //categoryData  日期  把返回的日期放到categoryData[]数组中
                    categoryData.push(rawData[i].splice(0, 1)[0]);
                    values.push(rawData[i])
                }
                return {
                    categoryData: categoryData, //数组中的日期 x轴对应的日期
                    values: values              //数组中的数据 y轴对应的数据
                };
            }
            var closes = [];

            for (var i = 0; i < b.length; i++) {
                closes.push(b[i][1]);
            }
            var ema = function (lastEma, closePrice, units) {
                return (lastEma * (units - 1) + closePrice * 2) / (units + 1);
            };
            var dea = function (lastDea, curDiff) {
                return (lastDea * 8 + curDiff * 2) / 10;
            };
            function  macd(ticks) {
                var ema12 = [], ema26 = [], diffs = [], deas = [], bars = [];
                for(var i = 0; i < ticks.length; i++) {
                    var c = ticks[i];
                    if (i == 0) {
                        ema12.push(c);
                        ema26.push(c);
                        deas.push(0);
                    } else {
                        ema12.push(ema(ema12[i-1], c, 12));
                        ema26.push(ema(ema26[i-1], c, 26));
                    }
                    diffs.push(ema12[i] - ema26[i]);
                    if (i != 0) {
                        deas.push(dea(deas[i-1], diffs[i]));
                    }
                    bars.push((diffs[i]-deas[i]) * 2);
                }
                return {diffs: diffs, deas: deas, bars: bars};
            };
            var mac = macd(closes);
            for (var t = [], a = [], n = [], e = [], s = 0; s < b.length; s++) {
                t.push(s);
                a.push(mac.bars[s])
                n.push(mac.diffs[s])
                e.push(mac.deas[s])
            }
            $(".macd2").html("DIF:" + (a[a.length - 1]).toFixed(2));
            $(".macd3").html("DEA:" + (n[n.length - 1]).toFixed(2));
            $(".macd1").html("MACD:"+(e[e.length -1]).toFixed(2));

            var c = [{
                data: n,
                color: "#fff"
            },
                {
                    data: e,
                    color: "#F4FD6E"
                }];
            initCharts3(t, a, c)

        }
    });
    this.ThreeTime = setInterval(function(){
        $.ajax({
            dataType: "json",
            /*url:"/app/public/klin",
        	data:{url:Url},*/
            url:Url,
            async: true,//请求是否异步，默认为异步，
            type: "GET",   //请求方式
            success: function (r) {
                //return;
                //请求成功时处理
                var b = [];
                var da = new Array();
                for (var i = 0; i < r.data.length-30; i++) {
                    /*var time = r.data[i].creattime;
                    var str1 = time.replace('_', '/');
                    var str2 = str1.replace('_', '/');*/
                	var str2=2019+i;
                    var da = new Array();
                    da.push(str2);
                    da.push(parseFloat(r.data[i].open))
                    da.push(parseFloat(r.data[i].close))
                    da.push(parseFloat(r.data[i].low))
                    da.push(parseFloat(r.data[i].high))
                    b.push(da);
                }
                var data0 = splitData(b.reverse());
                //切割数组，把数组中的日期和数据分离，返回数组中的日期和数据
                function splitData(rawData) {
                    var categoryData = [];
                    var values = [];
                    for (var i = 0; i < rawData.length; i++) {
                        //splice 返回每组数组中被删除的第一项，即返回数组中被删除的日期
                        //alert(rawData[i].splice(0, 1)[0]);
                        //categoryData  日期  把返回的日期放到categoryData[]数组中
                        categoryData.push(rawData[i].splice(0, 1)[0]);
                        values.push(rawData[i])
                    }
                    return {
                        categoryData: categoryData, //数组中的日期 x轴对应的日期
                        values: values              //数组中的数据 y轴对应的数据
                    };
                }
                var closes = [];

                for (var i = 0; i < b.length; i++) {
                    closes.push(b[i][1]);
                }
                var ema = function (lastEma, closePrice, units) {
                    return (lastEma * (units - 1) + closePrice * 2) / (units + 1);
                };
                var dea = function (lastDea, curDiff) {
                    return (lastDea * 8 + curDiff * 2) / 10;
                };
                function  macd(ticks) {
                    var ema12 = [], ema26 = [], diffs = [], deas = [], bars = [];
                    for(var i = 0; i < ticks.length; i++) {
                        var c = ticks[i];
                        if (i == 0) {
                            ema12.push(c);
                            ema26.push(c);
                            deas.push(0);
                        } else {
                            ema12.push(ema(ema12[i-1], c, 12));
                            ema26.push(ema(ema26[i-1], c, 26));
                        }
                        diffs.push(ema12[i] - ema26[i]);
                        if (i != 0) {
                            deas.push(dea(deas[i-1], diffs[i]));
                        }
                        bars.push((diffs[i]-deas[i]) * 2);
                    }
                    return {diffs: diffs, deas: deas, bars: bars};
                };
                var mac = macd(closes);
                for (var t = [], a = [], n = [], e = [], s = 0; s < b.length; s++) {
                    t.push(s);
                    a.push(mac.bars[s])
                    n.push(mac.diffs[s])
                    e.push(mac.deas[s])
                }
                $(".macd2").html("DIF:" + (a[a.length - 1]).toFixed(2));
                $(".macd3").html("DEA:" + (n[n.length - 1]).toFixed(2));
                $(".macd1").html("MACD:"+(e[e.length -1]).toFixed(2));

                var c = [{
                    data: n,
                    color: "#fff"
                },
                    {
                        data: e,
                        color: "#F4FD6E"
                    }];
                initCharts3(t, a, c)

            }
        });
    },1000*10);
}

var canvasImg2=function (url) {
    var _this=this;
    //分钟柱子
    clearInterval(this.TwoTime);
    $.ajax({
        url:"http://api.huobipro.com/market/history/kline?period=1day&size=100&symbol="+symbol,
//    	url:"/app/public/klin",
//    	data:{url:Url},
        type:"GET",
        dataType:'json',
        async: true,
        success:function(result){
            var t=[], Ec1=[];
            var data =result.data;
            for(var i=30,len=data.length;i<len;i++){
                t.push(i);
                Ec1.push(parseInt(data[i].vol));
            }
            var data1=(Ec1.reverse());
            initCharts2(t, data1);
        },
    });
    this.TwoTime = setInterval(function(){
        $.ajax({
            url:"http://api.huobipro.com/market/history/kline?period=1day&size=90&symbol="+symbol,
//        	url:"/app/public/klin",
//        	data:{url:Url},
        	type:"GET",
            dataType:'json',
            async: false,
            success:function(result){
                var t=[], Ec1=[];
                var data =result.data;
                for(var i=30,len=data.length;i<len;i++){
                    t.push(i);
                    Ec1.push(parseInt(data[i].vol));
                }
                var data1=(Ec1.reverse());
                initCharts2(t, data1);
            },
        });
    },1000*20);
}
var canvasImg=function (url) {
    var _this=this;
    clearInterval(this.ThreeTime);
    $.ajax({
        dataType: "json",
        url:"http://api.huobipro.com/market/history/kline?period=1day&size=90&symbol="+symbol,
//        url:"/app/public/klin",
//    	data:{url:url},
        async: false,//请求是否异步，默认为异步，
        type: "POST",   //请求方式
        success: function (r) {
            //return;
            //请求成功时处理
            var b = [];
            var da = new Array();
            for (var i = 0; i < r.datas.length; i++) {
                var time = r.datas[i].time;
                var str1 = time.replace('_', '/');
                var str2 = str1.replace('_', '/');
                var da = new Array();
                da.push(str2);
                da.push(parseFloat(r.datas[i].open))
                da.push(parseFloat(r.datas[i].close))
                da.push(parseFloat(r.datas[i].low))
                da.push(parseFloat(r.datas[i].high))
                b.push(da);
            }
            var data0 = splitData(b.reverse());
            //切割数组，把数组中的日期和数据分离，返回数组中的日期和数据
            function splitData(rawData) {
                var categoryData = [];
                var values = [];
                for (var i = 23; i < rawData.length; i++) {
                    //splice 返回每组数组中被删除的第一项，即返回数组中被删除的日期
                    //alert(rawData[i].splice(0, 1)[0]);
                    //categoryData  日期  把返回的日期放到categoryData[]数组中
                    categoryData.push(rawData[i].splice(0, 1)[0]);
                    values.push(rawData[i])
                }
                return {
                    categoryData: categoryData, //数组中的日期 x轴对应的日期
                    values: values              //数组中的数据 y轴对应的数据
                };
            }
            var closes = [];

            for (var i = 0; i < b.length; i++) {
                closes.push(b[i][1]);
            }
            var ema = function (lastEma, closePrice, units) {
                return (lastEma * (units - 1) + closePrice * 2) / (units + 1);
            };
            var dea = function (lastDea, curDiff) {
                return (lastDea * 8 + curDiff * 2) / 10;
            };
            function  macd(ticks) {
                var ema12 = [], ema26 = [], diffs = [], deas = [], bars = [];
                for(var i = 0; i < ticks.length; i++) {
                    var c = ticks[i];
                    if (i == 0) {
                        ema12.push(c);
                        ema26.push(c);
                        deas.push(0);
                    } else {
                        ema12.push(ema(ema12[i-1], c, 12));
                        ema26.push(ema(ema26[i-1], c, 26));
                    }
                    diffs.push(ema12[i] - ema26[i]);
                    if (i != 0) {
                        deas.push(dea(deas[i-1], diffs[i]));
                    }
                    bars.push((diffs[i]-deas[i]) * 2);
                }
                return {diffs: diffs, deas: deas, bars: bars};
            };
            var mac = macd(closes);
            for (var t = [], a = [], n = [], e = [], s = 30; s < b.length; s++) {
                t.push(s);
                a.push(mac.bars[s])
                n.push(mac.diffs[s])
                e.push(mac.deas[s])
            }
            $(".macd2").html("DIF:" + (a[a.length - 1]).toFixed(2));
            $(".macd3").html("DEA:" + (n[n.length - 1]).toFixed(2));
            $(".macd1").html("MACD:"+(e[e.length -1]).toFixed(2));

            var c = [{
                data: n,
                color: "#fff"
            },
                {
                    data: e,
                    color: "#F4FD6E"
                }];
            initCharts3(t, a, c)

        }
    });
    var ThreeTime = setInterval(function(){
        $.ajax({
            dataType: "json",
            url:"http://api.huobipro.com/market/history/kline?period=1day&size=90&symbol="+symbol,
//            url:"/kiln",
//            data:{url:"http://api.huobipro.com/market/history/kline?period=1day&size=90&symbol="+symbol},
            async: true,//请求是否异步，默认为异步，
            type: "GET",   //请求方式
            success: function (r) {
                //return;
                //请求成功时处理
            	console.log("echart3 inter",r)
                var b = [];
                var da = new Array();
                for (var i = 0; i < r.data.length; i++) {
                   /* var time = r.datas[i].time;
                    var str1 = time.replace('_', '/');*/
                    var str2 = 2018+i;
                    var da = new Array();
                    da.push(str2);
                    da.push(parseFloat(r.datas[i].open));
                    da.push(parseFloat(r.datas[i].close));
                    da.push(parseFloat(r.datas[i].low));
                    da.push(parseFloat(r.datas[i].high));
                    b.push(da);
                }
                var data0 = splitData(b.reverse());
                //切割数组，把数组中的日期和数据分离，返回数组中的日期和数据
                function splitData(rawData) {
                    var categoryData = [];
                    var values = [];
                    for (var i = 23; i < rawData.length; i++) {
                        //splice 返回每组数组中被删除的第一项，即返回数组中被删除的日期
                        //alert(rawData[i].splice(0, 1)[0]);
                        //categoryData  日期  把返回的日期放到categoryData[]数组中
                        categoryData.push(rawData[i].splice(0, 1)[0]);
                        values.push(rawData[i])
                    }
                    return {
                        categoryData: categoryData, //数组中的日期 x轴对应的日期
                        values: values              //数组中的数据 y轴对应的数据
                    };
                }
                var closes = [];

                for (var i = 0; i < b.length; i++) {
                    closes.push(b[i][1]);
                }
                var ema = function (lastEma, closePrice, units) {
                    return (lastEma * (units - 1) + closePrice * 2) / (units + 1);
                };
                var dea = function (lastDea, curDiff) {
                    return (lastDea * 8 + curDiff * 2) / 10;
                };
                function  macd(ticks) {
                    var ema12 = [], ema26 = [], diffs = [], deas = [], bars = [];
                    for(var i = 0; i < ticks.length; i++) {
                        var c = ticks[i];
                        if (i == 0) {
                            ema12.push(c);
                            ema26.push(c);
                            deas.push(0);
                        } else {
                            ema12.push(ema(ema12[i-1], c, 12));
                            ema26.push(ema(ema26[i-1], c, 26));
                        }
                        diffs.push(ema12[i] - ema26[i]);
                        if (i != 0) {
                            deas.push(dea(deas[i-1], diffs[i]));
                        }
                        bars.push((diffs[i]-deas[i]) * 2);
                    }
                    return {diffs: diffs, deas: deas, bars: bars};
                };
                var mac = macd(closes);
                for (var t = [], a = [], n = [], e = [], s = 30; s < b.length; s++) {
                    t.push(s);
                    a.push(mac.bars[s])
                    n.push(mac.diffs[s])
                    e.push(mac.deas[s])
                }
                /*$(".border-One span").html("DIF:" + (a[a.length - 1]).toFixed(2));
                $(".border-Two span").html("DEA:" + (n[n.length - 1]).toFixed(2));
                $(".border-Three span").html("MACD:"+(e[e.length -1]).toFixed(2));*/
                $(".macd2").html("DIF:" + (a[a.length - 1]).toFixed(2));
                $(".macd3").html("DEA:" + (n[n.length - 1]).toFixed(2));
                $(".macd1").html("MACD:"+(e[e.length -1]).toFixed(2));
                var c = [{
                    data: n,
                    color: "#fff"
                },
                    {
                        data: e,
                        color: "#F4FD6E"
                    }];
                initCharts3(t, a, c);
            }
        });
    },1000*6);
}

var Url="http://api.huobipro.com/market/history/kline?period=1day&size=100&symbol="+symbol;//一分钟
//"/app/index/klingdb?bigkey=BSbtcusd&key=FiveM" 5分钟
// Url=baseUrl+"/app/index/klingdb?bigkey=BSbtcusd&key=SixtyM" 1小时
//Url = baseUrl+"/app/index/klingdb?bigkey=BSbtcusd&key=Day"; 日线
canvasKimg(Url);
canvasImg2(Url);
canvasImg3(Url);
//预先加载 chart-box
$().ready(function(){
	setTimeout(function(){
		var height=$("#Echart2").css("height")
		$(".echart-box").css("height",height);
		$(".echart2-text").css("display","block");
		$(".echart3-text").css("display","none");
	},500);
	//时间选择的切换效果
	$(".Container_Time").on("click","td",function(e){
		//e.stopPropata
		$(this).addClass("curr").siblings(".curr").removeClass("curr");
	})
})
//日线
var showEcharts=0;
$("#dayLine").on("click", function () {
    Url = "http://api.huobipro.com/market/history/kline?period=1day&size=100&symbol="+symbol;
    showEcharts=0;
    canvasKimg(Url)
    canvasImg2(Url);
    canvasImg3(Url);
})



$("#Echarts1").on("click",function(){
    window.showEcharts++;
    canvasKimg(Url)
});
//*****************************************************************************************app_apI
var echart=0;
$(".echart-box").on("click",function(){
	echart++;
	var height=$("#Echarts2").css("height");
	height=parseInt(height)+15+"px";height="-"+height;
	if(echart%2==1){
		$("#Echarts2").css("top",height);
		$("#Echarts3").css("top",height);
		$(".echart2-text").css("display","none");
		$(".echart3-text").css("display","block");
	}else{
		$("#Echarts2").css("top","0");
		$("#Echarts3").css("top","0");
		$(".echart2-text").css("display","block");
		$(".echart3-text").css("display","none");
	}
	
})


var userid=localStorage.getItem("userid");
var type=sessionStorage.getItem("PROD");
var BTC={};




